using System;
using System.Xml;

namespace Mehat.San.SanFS
{	
	public class ConfigurationManager
	{	
		private static IDataStoreStrategy	mDataStoreStrategy = null;
		private static Cache.ICachePolicy	mCachePolicy = null;
		
		public static void Initialize()
		{
			XmlDocument	doc = new XmlDocument();
			
			doc.Load("/etc/sanfs_cfg.xml");
			XmlNode	sanfsNode = doc.GetElementsByTagName("sanfs")[0];
			
			foreach (XmlNode node in sanfsNode.ChildNodes)
			{
				if (node.Name.ToLower() == "vmount")
				{
					if (mDataStoreStrategy == null)
						throw new Exception("DataStore not yet initialized");
					IVirtualMount	vmnt = VirtualMountManager.Create(node);
					
					vmnt.Attach();
				}
				else if (node.Name.ToLower() == "datastore")
				{
					mDataStoreStrategy = DataStoreFactory.CreateStrategy(node);
					mDataStoreStrategy.Connect();
				}
				else if (node.Name.ToLower() == "cache")
				{
					// FIXME: Fix xml issue
					//Cache.CacheFactory.Initialize(node);
					//mCachePolicy = Cache.CacheFactory.ActivePolicy;
				}
				else
					throw new Exception(String.Format("Unknown xml element name '{0}'", node.Name));
			}
		}
		
		public static IDataStoreStrategy DataStoreStrategy
		{
			get { return mDataStoreStrategy; }
		}
		
		public static Cache.ICachePolicy ActiveCachePolicy
		{
			get { return mCachePolicy; }
		}
	}	// END CLASS
}	// END NAMESPACE
